<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="70px"
    >
      <el-form-item label="访问码" prop="accessCode">
        <el-input
          v-model="queryParams.accessCode"
          placeholder="请输入访问码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信息来源" prop="source">
        <el-select
          v-model="queryParams.source"
          placeholder="信息来源"
          clearable
        >
          <el-option
            v-for="dict in sourceOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" border :data="tableList">
      <el-table-column label="访客" align="center" prop="accessUserName" />
      <!-- <el-table-column label="访问内容" align="center" prop="accessContent" /> -->
      <el-table-column label="访问码" align="center" prop="accessCode" />
      <!-- <el-table-column label="访问开始时间" align="center" prop="beginTime" /> -->
      <!-- <el-table-column label="访问结束时间" align="center" prop="endTime" /> -->
      <!-- <el-table-column label="观看时长" align="center" prop="accessInterval">
        <template slot-scope="scope">
          {{ (scope.row.accessInterval / 60).toFixed(2) }}分钟
        </template>
      </el-table-column> -->
      <el-table-column label="信息来源" align="center" prop="source">
        <template slot-scope="scope">
          {{
            getValue(sourceOptions, scope.row.source)
              ? getValue(sourceOptions, scope.row.source)
              : scope.row.source
          }}
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="submitUserName" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleDetail(scope.row)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="详情"
      :visible.sync="detailOpen"
      width="90%"
      append-to-body
      destroy-on-close
    >
      <Detail :accessCode="detailCode"></Detail>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from "@/api/visitorManagement/index";
import Detail from "./components/detail.vue";
export default {
  name: "VisitorManagement",
  components: { Detail },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accessCode: undefined,
        source: undefined,
      },
      // 创建时间范围
      dateRange: [],
      // 视频来源
      sourceOptions: [{ value: "0", label: "视频详情" }],
      detailOpen: false,
      detailCode: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getList({
        ...this.queryParams,
        beginTime: this.dateRange[0],
        endTime: this.dateRange[1],
      }).then((response) => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表格根据value获取label
    getValue(options, value) {
      return options.find((item) => item.value === value)?.label;
    },
    // 查看详情
    handleDetail(row) {
      this.detailOpen = true;
      this.detailCode = row.accessCode;
    },
  },
};
</script>
